﻿/* 
 *  <copyright file="PathNode.cs" company="Wesley Jowitt">
 *  
 *  SobrietyEngine: Copyright (c) 2011 Wesley Jowitt
 *  
 *  This software is distributed under a software license.
 *   * As of the creation time of this file, the license in use is the Microsoft Permissive License (Ms-PL) 
 *    * The license can be viewed at:
 *     * License.Txt in the root folder of this project
 *      * http://sobrietyengine.codeplex.com/license
 *       * http://www.microsoft.com/opensource/licenses.mspx
 *       
 *  </copyright>
 *   */

using System;
using System.Collections.Generic;

using Microsoft.Xna.Framework;

using SobrietyEngine.Actor;
using SobrietyEngine.PathFinding;

namespace SobrietyEngine.Components.Paths
{
    public class PathNodeCmp: BaseComponent, SobrietyEngine.Components.IComponent
    {

        public INode PathNode { get; private set; }

        IPosition myPos;
        public Vector2 PositionV2 { get { return myPos.PositionV2; } }
        public Vector3 PositionV3 { get { return myPos.PositionV3; } }

        public PathNodeCmp()
            : base()
        {
        }

        public PathNodeCmp(INode linkedPathNode)
            : base()
        {
            PathNode = linkedPathNode;
        }

        public override Boolean InitComponent()
        {
            myPos = Actor.Get<IPosition>();
            if (myPos == null)
            {
                Logging.Logger.GetSingleton().WriteError(this, "Couldnt find PositionCmp on parent actor");
                return false;
            }
            return base.InitComponent();   
        }

        public List<IActor> GetNextPathNodeActors()
        {
            List<IActor> next = new List<IActor>();
            List<IEdge> edges = PathNode.GetOutEdges();
            foreach (IEdge edge in edges)
            {
                INode other = edge.GetOtherNode(PathNode);
                IActor otherAct = (IActor)other.UserData;
                next.Add(otherAct);
            }
            return next;
        }

        public List<PathNodeCmp> GetNextPathNodeComponentss()
        {
            List<PathNodeCmp> next = new List<PathNodeCmp>();
            List<IEdge> edges = PathNode.GetOutEdges();
            foreach (IEdge edge in edges)
            {
                INode other = edge.GetOtherNode(PathNode);
                IActor otherAct = (IActor)other.UserData;
                next.Add(otherAct.Get<PathNodeCmp>());
            }
            return next;
        }
    }
}
